package com.program.houai.advice;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.houbb.opencc4j.util.ZhConverterUtil;
import com.program.houai.exception.ErrorType;
import com.program.houai.util.PageUtils;
import com.program.houai.util.R;
import com.program.houai.vo.ComboxVO;
import com.program.houai.vo.UserOrderVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.MethodParameter;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.List;
import java.util.Map;

@RestControllerAdvice(basePackages = {"com.topcheer.lmp.*.controller"}, annotations = {RestController.class})
public class ResponseAdvice implements ResponseBodyAdvice<Object> {
    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return !returnType.getDeclaringClass().getName().contains("springfox");
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        if (body instanceof R) {
            //异常情况
            if (!StringUtils.isBlank(((R) body).getMsg())){
                ((R) body).setMsg(ZhConverterUtil.toTraditional(((R) body).getMsg()));
            }
/*            String url = request.getURI().getPath();
            //下拉框
            if (url.equals("/app/vipcard/comboxList")){
                Map<String, List<ComboxVO>> allMap = (Map<String, List<ComboxVO>>) ((R) body).getData();

            }*/
       /*     if (((R) body).getData() instanceof PageUtils){
              *//*  if (url.equals("")){
                }
                PageUtils<UserOrderVO> customObj = (PageUtils<UserOrderVO>) ((R) body).getData();*//*
                System.out.println("111");
            }*/

            return body;
        }else if(body instanceof String){
            try {
                return new ObjectMapper().writeValueAsString(R.ok(body));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }else if(body instanceof InputStreamResource || body instanceof FileSystemResource){
            return body; // 直接返回原始响应体
        }else if(body instanceof Throwable){
            return R.failed(ErrorType.BIZ_ERROR,"接口调用出错，请联系管理员！");
        }
        return R.ok(body);
    }

    public static void main(String[] args) {
        String  name =  ZhConverterUtil.toTraditional("中国");
        System.out.println(name);
        String  name1 =  ZhConverterUtil.toTraditional(name);
        System.out.println(name1);
    }
}

